<?php


namespace app\index\controller;
use app\index\model\Cate as IndexCate;
use think\Controller;

class Common extends Controller
{
    public function _initialize()
    {
        //获取菜单栏目
        $this->getCateNaves();
        //获取当前位置
        if(input('cateid')){
            $address=$this->bread(input('cateid'));
            $this->assign("bread",$address);
        }else if(input('pageid')){
            $address=$this->bread(input('pageid'));
            $this->assign("bread",$address);    
        };
    }
    public function getCateNaves(){
        $cates=db("cate")->where(array('pid'=>0))->select();
        foreach ($cates as $k=> $v){
            $chindren=db("cate")->where(array('pid'=>$v['id']))->select();
            if($chindren){
                $cates[$k]['children']=$chindren;
            }else{
                $cates[$k]['children']=0;
            }
        }
        $this->assign("cateres",$cates);
    }
    public function bread($cateid){
        $cates=new IndexCate();
        $posArr=$cates->getParentId($cateid);
        $postion=array();
        foreach ($posArr as $k=>$v){
            $data=db('cate')->where('id',$v)->find();
            $addr=$data['catename'];
            $postion[]=$addr;
        }
//        return implode(">>",$postion);
        return $postion;
    }
}